﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace WebApplication1.Libs
{
    public class ThoiDiemBaoCao
    {
        public static void ThietLapKhoangThoiGianBaoCao(string kyBaoCao,
                                       DevExpress.Web.ASPxEditors.ASPxComboBox cmbKyBaoCao,
                                        DevExpress.Web.ASPxEditors.ASPxDateEdit dtThoiDiemDau,
                                        DevExpress.Web.ASPxEditors.ASPxDateEdit dtThoiDiemCuoi)
        {
            try
            {
                DateTime ngayBatDau = DateTime.Now;
                DateTime ngayKetThuc = DateTime.Now;
                DateTime now = GetSystemDateTime();

                switch (kyBaoCao)
                {
                    case "Hôm nay":
                        dtThoiDiemDau.Enabled = true;
                        dtThoiDiemCuoi.Enabled = true;
                        ngayBatDau = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
                        ngayKetThuc = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
                        break;
                    case "Hôm qua":
                        dtThoiDiemDau.Enabled = true;
                        dtThoiDiemCuoi.Enabled = true;
                        ngayBatDau = new DateTime(DateTime.Now.AddDays(-1).Year, DateTime.Now.AddDays(-1).Month, DateTime.Now.AddDays(-1).Day, 0, 0, 0);
                        ngayKetThuc = new DateTime(DateTime.Now.AddDays(-1).Year, DateTime.Now.AddDays(-1).Month, DateTime.Now.AddDays(-1).Day, 23, 59, 59);
                        break;
                    case "Tháng này":
                        dtThoiDiemDau.Enabled = true;
                        dtThoiDiemCuoi.Enabled = true;
                        ngayBatDau = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
                                                DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month), 23, 59, 59);
                        break;
                    case "Cả năm":
                        dtThoiDiemDau.Enabled = false;
                        dtThoiDiemCuoi.Enabled = false;
                        ngayBatDau = new DateTime(DateTime.Now.Year, 1, 1);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59);
                        break;
                    case "Quý I":
                        dtThoiDiemDau.Enabled = false;
                        dtThoiDiemCuoi.Enabled = false;
                        ngayBatDau = new DateTime(DateTime.Now.Year, 1, 1);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, 3, 31, 23, 59, 59);
                        break;
                    case "Quý II":
                        dtThoiDiemDau.Enabled = false;
                        dtThoiDiemCuoi.Enabled = false;
                        ngayBatDau = new DateTime(DateTime.Now.Year, 4, 1);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, 6, 30, 23, 59, 59);
                        break;
                    case "Quý III":
                        dtThoiDiemDau.Enabled = false;
                        dtThoiDiemCuoi.Enabled = false;
                        ngayBatDau = new DateTime(DateTime.Now.Year, 7, 1);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, 9, 30, 23, 59, 59);
                        break;
                    case "Quý IV":
                        dtThoiDiemDau.Enabled = false;
                        dtThoiDiemCuoi.Enabled = false;
                        ngayBatDau = new DateTime(DateTime.Now.Year, 10, 1);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59);
                        break;
                    case "Tùy chọn":
                    case "":
                    default:
                        if (kyBaoCao.StartsWith("Tháng "))
                        {
                            try
                            {
                                dtThoiDiemDau.Enabled = false;
                                dtThoiDiemCuoi.Enabled = false;
                                string strThang = kyBaoCao.Substring(kyBaoCao.LastIndexOf(" "));
                                int thang = int.Parse(strThang);

                                ngayBatDau = new DateTime(now.Year, thang, 1);
                                ngayKetThuc = new DateTime(now.Year, thang,
                                                DateTime.DaysInMonth(now.Year, thang), 23, 59, 59);
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Trace.WriteLine(ex.ToString());
                            }
                        }
                        else
                        {
                            dtThoiDiemDau.Enabled = true;
                            dtThoiDiemCuoi.Enabled = true;
                            //now = GetSystemDateTime();
                            //ngayBatDau = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
                            //ngayKetThuc = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
                        }

                        break;
                }
                dtThoiDiemDau.Date = ngayBatDau;
                dtThoiDiemCuoi.Date = ngayKetThuc;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static DateTime GetSystemDateTime()
        {
            SqlConnection con = new SqlConnection(Connection._ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                return GetSystemDateTime(cmd);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
        public static DateTime GetSystemDateTime(SqlCommand cmd)
        {
            try
            {
                cmd.CommandText = "SELECT GETDATE()";
                DateTime retDate = (DateTime)cmd.ExecuteScalar();
                retDate.Subtract(new TimeSpan(0, 0, 0, 0, retDate.Millisecond));
                return retDate;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}